package com.fudan.gamestore.service.impl;

import com.fudan.gamestore.mapper.GameMapper;
import com.fudan.gamestore.mapper.OrderItemMapper;
import com.fudan.gamestore.mapper.OrderMapper;
import com.fudan.gamestore.pojo.Game;
import com.fudan.gamestore.pojo.Order;
import com.fudan.gamestore.pojo.OrderItem;
import com.fudan.gamestore.service.OrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderItemMapper orderItemMapper;
    @Autowired
    private GameMapper gameMapper;

    @Override
    public PageInfo<Order> getOrderByUserIdPage(Integer userId, Integer size, Integer pageNum) {
        PageHelper.startPage(pageNum, size);
        List<Order> orderList = orderMapper.getOrderByUserId(userId);
        return new PageInfo<>(orderList, 5);
    }

    @Override
    public List<OrderItem> getOrderItemsByOrderId(Integer orderId) {
        return orderItemMapper.getOrderItemsByOrderId(orderId);
    }

    @Override
    public PageInfo<Order> getAllOrderByPage(Integer size, Integer pageNum) {
        PageHelper.startPage(pageNum, size);
        List<Order> orderList = orderMapper.getAllOrder();
        return new PageInfo<>(orderList, 5);
    }

    @Override
    public int setOrderState(Integer orderId, Integer state) {
        return orderMapper.setOrderStateByOrderId(orderId, state);
    }

    @Override
    public int deleteOrderByOrderId(Integer orderId) {
        int orderNum = orderMapper.deleteOrderByOrderId(orderId);
        int orderItemNum = orderItemMapper.deleteOrderItemsByOrderId(orderId);
        return orderNum + orderItemNum;
    }

    @Override
    public Order getOrderByOrderId(Integer orderId) {
        return orderMapper.getOrderByOrderId(orderId);
    }

    @Override
    public List<Integer> getGameIdListByOrderId(Integer orderId) {
        return orderItemMapper.getGameIdListByOrderId(orderId);
    }

    @Override
    @Transactional
    public int insertOrder(Integer userId, List<Integer> gameIdList) {
        List<OrderItem> items = new ArrayList<>();
        Order order = new Order(userId, 0);
        int orderNum = orderMapper.insertOrder(order);
        int orderId = order.getOrderId();
        for (Integer integer : gameIdList) {
            items.add(new OrderItem(orderId, integer));
        }
        int orderItemsNum = orderItemMapper.insertOrderItems(items);
        int sum = 0;
        for (Integer integer : gameIdList) {
            Game game = gameMapper.getGameById(integer);
            sum += game.getPrice();
        }
        orderMapper.setOrderMoney(orderId, sum);
        return orderId;
    }

    @Override
    @Transactional
    public int finishOrderAfterPay(Integer orderId) {
        Order order = orderMapper.getOrderByOrderId(orderId);
        orderMapper.setOrderStateByOrderId(orderId, 1);
        List<Integer> gameIdList = orderItemMapper.getGameIdListByOrderId(orderId);
        gameMapper.addOwnGame(order.getUserId(), gameIdList);
        gameMapper.addGameSaleCount(gameIdList);
        return 1;
    }
}
